﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.XtraGrid.Demos.util;
using mendian;
using Luna小店.DTO;
using Luna小店.UC.面料库存.库存记录.service;

namespace Luna小店.UC.面料库存
{
    public partial class Frm出库面料选择 : DevExpress.XtraEditors.XtraForm
    {
        public DTO采购分录 DTO采购分录 { get; set; }
        private String unitPrice;
        private String id;
        private String vendorName;
        private String materialNo;
        private String meterialDesc;
        private String composition;
        private String density;    //  纱织密度
        private String weight;    //  克重
        private String width;    //  幅宽
        private String thick;    //  厚度
        private String elastic;    //  弹力
        private String feature;    //  面料特性注意
        private String continuity;    //  延续性
        private String uasge;    //  用途
        private String inspectionResults;    //  验料结果
        private String color;    //  颜色
        private String season;    //  季节
        private String style;    //  风格
        private String arrangementMethod;    //  整理方式
        private String fabricQuanlity;
        public Frm出库面料选择()
        {
            InitializeComponent();
            this.searchLookUpEdit面料.Properties.DataSource = 面料记录Service.get面料一览();
        }
        #region 选择面料
        private void searchLookUpEdit面料_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit面料View_Click_1(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit面料.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.id = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.unitPrice = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "unitPrice").ToString();
                    this.vendorName = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "vendorName").ToString();
                    this.materialNo = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "materialNo").ToString();
                    this.meterialDesc = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "meterialDesc").ToString();
                    this.composition = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "composition").ToString();
                    this.density = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "density").ToString();   //  纱织密度
                    this.weight = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "weight").ToString();   //  克重
                    this.width = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "width").ToString();   //  幅宽
                    this.thick = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "thick").ToString();   //  厚度
                    this.elastic = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "elastic").ToString();   //  弹力
                    this.feature = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "feature").ToString();   //  面料特性注意
                    this.continuity = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "continuity").ToString();   //  延续性
                    this.uasge = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "uasge").ToString();   //  用途
                    this.inspectionResults = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "inspectionResults").ToString();   //  验料结果
                    this.color = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "color").ToString();   //  颜色
                    this.season = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "season").ToString();   //  季节
                    this.style = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "style").ToString();   //  风格
                    this.arrangementMethod = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "arrangementMethod").ToString();   //  整理方式
                    this.fabricQuanlity = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "fabricQuanlity").ToString();      //库存数量

                    this.textEdit库存移动加权平均价格.Text = this.unitPrice;
                    this.textEdit面料号.Text = this.materialNo;
                    this.textEdit描述名称.Text = this.meterialDesc;
                    this.textEdit成分.Text = this.composition;
                    this.textEdit供货商.Text = this.vendorName;
                    this.textEdit纱织密度.Text = this.density;
                    this.textEdit克重.Text = this.weight;
                    this.textEdit幅宽.Text = this.width;
                    this.textEdit厚度.Text = this.thick;
                    this.textEdit弹力.Text = this.elastic;
                    this.textEdit面料特性注意.Text = this.feature;
                    this.textEdit用途.Text = this.uasge;
                    this.textEdit延续性.Text = this.continuity;
                    this.textEdit验料结果.Text = this.inspectionResults;
                    this.textEdit颜色.Text = this.color;
                    this.textEdit季节.Text = this.season;
                    this.textEdit风格.Text = this.style;
                    this.textEdit整理方式.Text = this.arrangementMethod;
                    this.textEdit库存数量.Text = this.fabricQuanlity;
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit面料.ToolTip = null;
            searchLookUpEdit面料.EditValue = null;
        }
        private void searchLookUpEdit面料_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.meterialDesc;
        }
        #endregion

        private void simpleButton提交_Click(object sender, EventArgs e)
        {
            if (!FunctionHelper.Is大于0(this.textEdit出库数量.Text))
            {
                MessageBox.Show("出库数量必须大于0！");
                return;
            }
            if (String.IsNullOrEmpty(this.id))
            {
                return;
            }
            this.DTO采购分录 = new DTO采购分录(
                this.id
                , this.materialNo
                , this.meterialDesc
                , this.composition
                , this.width
                , Convert.ToDouble(this.textEdit出库数量.Text)
                , this.textEdit库存移动加权平均价格.Text);
            this.Close();
        }

        private void textEdit出库数量_Validating(object sender, CancelEventArgs e)
        {
            if (!FunctionHelper.Is大于0(this.textEdit出库数量.Text))
            {
                MessageBox.Show("出库数量必须大于0！");
                return;
            }
        }
    }
}